package com.momo.service.product;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.momo.common.common.JSONResult;
import com.momo.common.req.product.BugReq;
import com.momo.common.req.sysmain.RedisUser;
import com.momo.common.util.DateUtil;
import com.momo.mapper.dao.BugDAO;
import com.momo.mapper.dataobject.BugDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by MOMO on 2019/4/25.
 */
@Service
@Slf4j
public class SysBugServiceImpl implements SysBugService {
    @Autowired
    private BugDAO bugDAO;
    @Value("${momo.static}")
    private String staticImg;

    @Override
    public JSONResult bugPage(BugReq bugReq, RedisUser redisUser) {
        BugDO entity = new BugDO();
        BeanUtils.copyProperties(bugReq, entity);
        PageHelper.startPage(bugReq.getPageNum(), bugReq.getPageSize(), "id desc");
        List<BugDO> list = bugDAO.getBugList(entity);
        PageInfo<BugDO> pageInfo = new PageInfo<>(list);
        pageInfo.getList().forEach(bugDO -> {
            bugDO.setSysProductImg(staticImg + bugDO.getSysProductImg());
        });
        return JSONResult.ok(pageInfo);
    }

    @Override
    public JSONResult save(BugReq bugReq, RedisUser redisUser) {
        BugDO entity = new BugDO();
        BeanUtils.copyProperties(bugReq, entity);
        entity.setCreateBy(redisUser.getSysUserName());
        entity.setUpdateBy(redisUser.getSysUserName());
        entity.setCreateTime(DateUtil.getDateTime());
        entity.setUpdateTime(DateUtil.getDateTime());
        bugDAO.insert(entity);
        return JSONResult.ok("新增产品提优成功");
    }
}
